﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Runtime.Serialization;
using System.Data.Objects;
using System.Text.RegularExpressions;


namespace GTServices.Entities
{

    public partial class Pattern : BaseEntity
    {

        [Key]
        public Guid Id { get; set; } 
        
        public string PatternStr { get; set; }

        /// <summary>
        /// Why is this pattern here? 
        /// </summary>
        public string PatternInfo { get; set; }
        public string ReferenceUrl { get; set; } 
        public string Recommendation { get; set; }

        public bool IsSelected { get; set; }

        //public virtual Issue    Issue    { get; set; } 
        public virtual Language Language { get; set; }

        
        [IgnoreDataMember]
        public virtual ObservableCollection<Category> Categories { get; set; } 

        public Pattern()
        {
            this.Id = Guid.NewGuid();
            this.Recommendation = "To fix this..";
            this.ReferenceUrl = "http://someplace.somewhere/Something/";
            this.PatternStr = "Default Pattern String Text";
            this.Categories = new ObservableCollection<Category>(); 
        }

        public override void Validate()
        {
            if (String.IsNullOrEmpty(PatternStr))
            {
                throw new ArgumentException("Null or Empty Pattern");
            }
        }

        public bool IsValidPattern()
        {
                try
                {
                    Regex.Match("", this.PatternStr);
                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
        }

        public override bool Equals(object obj)
        {
            if (!(obj is Pattern))
            {
                return false;
            }
            return (obj as Pattern).Id == this.Id;
        }

        public override string ToString()
        {
            return PatternStr;
        }
    }
   
}
